#include "CApp.h"
#include <iostream> 
#include <ctime> 
#include <cstdlib>
 
bool CApp::OnInit() {
    if(SDL_Init(SDL_INIT_EVERYTHING) < 0) {
        return false;
    }
 
    if((Surf_Display = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF)) == NULL) {
        return false;
	}

	// Initialize SDL_ttf library
	/*
    if (TTF_Init() != 0)
	{
		return false;
	}

	// Load a font
	
	font = TTF_OpenFont("FreeSans.ttf", 14);
	if (font == NULL)
	{
		return false;
	}
    */
	 
    if((Surf_Test = CSurface::OnLoad("..//SDL_Tutorial//Images//screen.bmp")) == NULL) {
        return false;
    }
	// Define the transparent color
	SDL_SetColorKey( Surf_Test, SDL_SRCCOLORKEY, SDL_MapRGB(Surf_Test->format, 255, 0, 255) );

	if((Bkgrnd_bmp = CSurface::OnLoad("..//SDL_Tutorial//Images//mariobackground.bmp")) == NULL) {
        return false;
    }

	Yoshi_1.Init(100,100);
	Yoshi_2.Init(200,100);

	// Just an example of using random numbers ? Not used ?
	srand((unsigned)time(0)); 
    int random_integer; 
    int lowest=0, highest=460; 
    int range=(highest-lowest)+1; 
    for(int index=0; index<20; index++){ 
        random_integer = lowest+int(range*rand()/(RAND_MAX + 1.0)); 
    } 

	for(int i=0; i<NO_OF_YOSHIS; /*number of objects*/ i++)
	{
		int x,y;

		x = lowest+int(620*rand()/(RAND_MAX + 1.0));
		y = 179; //lowest+int(460*rand()/(RAND_MAX + 1.0));
		Yoshi_objects[i] = new Yoshi();
		Yoshi_objects[i]->Init(x,y);

		Yoshi_objects[i]->SetCurrentFrame( lowest+int(8*rand()/(RAND_MAX + 1.0)) );

		Yoshi_objects[i]->SetFrameRate( 20+int((200-20)*rand()/(RAND_MAX + 1.0)) );

		Yoshi_objects[i]->SetDirection( 1+int((2)*rand()/(RAND_MAX + 1.0)) );

		//Yoshi_objects[i]->SetFrameRate(200);
		//Yoshi_objects[i]->SetDirection(DIR_RIGHT);

	}



	Anim_Yoshi.MaxFrames = 8;
 
    return true;
}